package com.zenith.edu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zenith.edu.mapper.EduForumTopicMapper;
import com.zenith.edu.po.domain.EduForumTopic;
import com.zenith.edu.po.dto.EduForumTopicDTO;
import com.zenith.edu.po.vo.EduForumTopicVO;
import com.zenith.edu.service.EduForumTopicService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EduForumTopicServiceImpl extends ServiceImpl<EduForumTopicMapper, EduForumTopic> implements EduForumTopicService {
    
    @Override
    public EduForumTopicVO createForumTopic(EduForumTopicDTO dto) {
        EduForumTopic forumTopic = new EduForumTopic();
        BeanUtils.copyProperties(dto, forumTopic);
        this.save(forumTopic);
        
        EduForumTopicVO vo = new EduForumTopicVO();
        BeanUtils.copyProperties(forumTopic, vo);
        return vo;
    }
    
    @Override
    public EduForumTopicVO getForumTopicById(Long id) {
        EduForumTopic forumTopic = this.getById(id);
        if (forumTopic == null) {
            return null;
        }
        
        EduForumTopicVO vo = new EduForumTopicVO();
        BeanUtils.copyProperties(forumTopic, vo);
        return vo;
    }
    
    @Override
    public EduForumTopicVO updateForumTopic(EduForumTopicDTO dto) {
        EduForumTopic forumTopic = new EduForumTopic();
        BeanUtils.copyProperties(dto, forumTopic);
        this.updateById(forumTopic);
        
        EduForumTopicVO vo = new EduForumTopicVO();
        BeanUtils.copyProperties(forumTopic, vo);
        return vo;
    }
    
    @Override
    public boolean deleteForumTopic(Long id) {
        return this.removeById(id);
    }
    
    @Override
    public List<EduForumTopicVO> getForumTopicList() {
        List<EduForumTopic> forumTopicList = this.list();
        return forumTopicList.stream().map(forumTopic -> {
            EduForumTopicVO vo = new EduForumTopicVO();
            BeanUtils.copyProperties(forumTopic, vo);
            return vo;
        }).collect(Collectors.toList());
    }
}